<?php
/**
 *
 * This file contains {@link Domain51_Service_Amazon_S3}
 *
 *
 * @package Domain51
 *
 * @subpackage Domain51_Service
 *
 * @author Travis Swicegood <development [at] domain51 [dot] com>
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 */
 

/**
 *
 * Load require super-class
 *
 */
Solar::loadClass('Solar_Base');


/**
 *
 * Provides an interface to Amazon's Simple Storage System (S3).
 *
 *
 * This provides a native Solar API to Amazon's S3 web service.  You instantiate
 * this object using the {@link Solar::factory()}.
 *
 * Your Amazon Access Key and Secret Key must be part of your configuration or 
 * passed in to this object on instantiation.  Sample configuration as part of
 * your Solar.config.php:
 *  <code>
 *      $config['Domain_Service_Amazon_S3'] = array(
 *          'access_key' => '12345',
 *          'secret_key' => 'super secret hash'
 *      );
 *  </code>
 *
 * For information on how to obtain keys, please visit the 
 * {@link http://aws.amazon.com/s3 Amazon S3} webpage. 
 *
 *
 * <i>NOTE</i>: This is still under development, so API documents are being kept
 * purposely small so they do not become obsoleted.  Full documentations will be
 * provided in this space as soon as this object stablizes.  For an example of
 * how this class can be used, see {@link Domain51_App_S3}
 *
 *
 * @package Domain51
 *
 * @subpackage Domain51_Service
 *
 * @author Travis Swicegood <development [at] domain51 [dot] com>
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 */
class Domain51_Service_Amazon_S3 extends Solar_Base
{
    /**
     *
     * Provides the default values for internal configuration
     *
     * Keys are:
     *  `access_key` : (string)
     *      The Amazon provided S3 Access Key
     *
     *  `secret_key` : (string)
     *      The Amazon provided S3 Secret Key
     *
     *
     * @var array
     *
     * @access protected
     *
     */
    protected $_Domain51_Service_Amazon_S3 = array(
        'access_key' => '',
        'secret_key' => '',
        'connection_type' => 'pecl'
    );
    
    
    private $_connection = null;
    
    
    /**
     *
     * Handle instantiation
     *
     *
     * @param array $config
     *
     */
    public function __construct($config = null)
    {
        if (!extension_loaded('http')) {
            throw $this->_exception(
                'ERR_EXTENSION_NOT_LOADED',
                array('missing_extension' => 'pecl/http')
            );
        }
        parent::__construct($config);
        
        $authGen = Solar::factory(
            'Domain51_Service_Amazon_Util_AuthorizationGenerator',
            $this->_config
        );
        
        $headerConfig = array(
            'authGen' => $authGen
        );
        $header = Solar::factory(
            'Domain51_Service_Amazon_Util_Header',
            $headerConfig
        );
        
        $this->_connection = Solar::factory(
            'Domain51_Service_Amazon_Util_Connection',
            array_merge($this->_config, array('header' => $header))
        );
    }
    
    
    /**
     *
     * Provide a Strategy to load various PHP objects to handle generating
     * requests to S3.
     *
     * <i>Note</i>: This is always invoked via PHP 5's magic methods, and never
     * directly.
     *
     *
     * @param string $method
     *
     * @return object
     *
     */
    public function __get($method) 
    {
        $method = strtolower($method);
        switch ($method) {
        case 'bucket' :
        case 'object' :
        case 'service' :
            $adapter = ucfirst($method);
            break;
        
        default:
            throw $this->_exception(
                'ERR_UNKNOWN_PROPERTY',
                array('property' => $method)
            );
        }
        
        $adapterClass = 'Domain51_Service_Amazon_S3_' . $adapter;
        $params = array(
            'connection' => $this->_connection
        );
        return Solar::factory($adapterClass, $params); 
    }
    
    
    /**
     *
     * Creates and returns a container for various types of requests.
     *
     * Currently, this only creates an "object" type container, but will be
     * expanded to create "buckets" as well.
     *
     *
     * @param string $type Currently only "object" is a valid parameter
     *
     * @return object
     *  $type == 'object' returns: 
     *      {@link Domain51_Service_Amazon_S3_Value_Object}
     *
     *
     * @todo Implement 'bucket' type
     */
    public function create($type) {
        $type = strtolower($type);
        switch ($type) {
        case 'object':
            $adapter = ucfirst($type);
            break;
        
        default:
            throw $this->_exception(
                'ERR_CONTAINER_NOT_IMPLEMENTED',
                array('container_type' => $type)
            );
        }
        
        return Solar::factory('Domain51_Service_Amazon_S3_Value_' . $adapter);
    }
}

?>